// pages/Census/data-detail/index.js
const Controllers = require('../../controllers/ControllerMy.js').controller;

const app = getApp();
var now = new Date();
const Y = now.getFullYear();
const M = now.getMonth() + 1 > 9 ? now.getMonth() + 1 : '0' + now.getMonth() + 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      pageNumber: 1,
      pageSize: 10,
    },
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true,
    commission: '',
    withdrawal: '',
    balance: '',
    agentStaffIncomeDetails: {},
    time: Y + '-' + M,
    endDate: Y + '-' + M

  },
  bindDateChange(e) {
    var that = this;
    console.log('bindDateChange--e==', e);
    that.setData({
      time: e.detail.value
    })
    that.agentStaffIncomePage();
  },
  agentStaffIncomePage() {
    var that = this;
    var page = that.data.page;
    var time = that.data.time;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    var params = { ...page, time };
    Controllers.agentStaffIncomePage(params).then(res => {
      console.log('agentStaffIncomePage--res==', res);
      const { code, data, desc } = res;
      const { page, commission, withdrawal, totalRow, lastPage } = data || {};
      const list = page.list || [];
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      // const balance = commission - withdrawal;
      const total = commission.cloud + commission.rent;
      const balance = total - withdrawal;
      commission.total = total ? parseFloat(total).toFixed(2) : 0;
      commission.balance = balance ? parseFloat(balance).toFixed(2) : 0;
      that.setData({
        list: list || [],
        commission,
        withdrawal,
        // balance: balance ? parseFloat(balance).toFixed(2) : 0,
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      that.setData({
        isLoading: false
      });
    })
  },

  agentStaffIncomeDetails() {
    var that = this;
    var page = that.data.page;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    var params = { ...page };
    if (page.pageNumber === 1) {
      params.onlyPage = 0;
    } else {
      params.onlyPage = 1;
    }
    Controllers.agentStaffIncomeDetails(params).then(res => {
      console.log('agentStaffIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { page, commission, withdrawal, totalRow, lastPage } = data || {};
      const list = page.list || [];
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      const total = commission.cloud + commission.rent;
      commission.total = total;
      console.log('commission==', commission);
      
      that.setData({
        commission,
        withdrawal,
        balance: balance ? parseFloat(balance).toFixed(2) : 0,
        list: list || [],
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      agentStaffId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.agentStaffIncomePage();
    // that.agentStaffIncomeDetails();
  },
  changeTab(e) {
    console.log(e);
    
    var that = this;
    var layer = parseInt(e.currentTarget.dataset.layer);
    var index = parseInt(e.currentTarget.dataset.index);
    if (layer === 1) {
      that.data.active = index;
    } else if (layer === 2) {
      that.data.activeII = index;
    }
    that.setData({
      active: that.data.active,
      activeII: that.data.activeII
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.isLoading || that.data.lastPage) {
    return;
    }
    that.data.page.pageNumber += 1;
    // that.agentStaffIncomeDetails();
    that.agentStaffIncomePage();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})